<template>
  <div class="">
    <el-table :data="filterTableData" style="width: 100%">
      <el-table-column
        align="center"
        fixed
        prop="name"
        label="姓名"
        width="150"
      />
      <el-table-column
        align="center"
        prop="people_zhanghao"
        label="用户名"
        width="150"
      />
      <el-table-column
        align="center"
        prop="people_password"
        label="密码"
        width="150"
      />
      <el-table-column align="center" prop="gender" label="性别" width="120" >
        <template #default="scope">
          <span>{{ scope.row.gender==1?'男':'女' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="avatur" label="头像" width="120">
        <template #default="scope">
          <el-avatar shape="square" :size="50" :src="scope.row.avatur" />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="is_service1"
        label="是否服务1"
        width="120"
      >
      <template #default="scope">
          <span>{{ scope.row.is_service1==1?'是':'否' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="is_service1"
        label="是否服务2"
        width="120"
      >
      <template #default="scope">
          <span>{{ scope.row.is_service1==2?'是':'否' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="is_service1"
        label="是否服务3"
        width="120"
      >
      <template #default="scope">
          <span>{{ scope.row.is_service2==3?'是':'否' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="introduction"
        label="自我介绍"
        width="400"
      />
      <el-table-column
        align="center"
        prop="address"
        label="服务区域"
        width="200"
      />
      <el-table-column
        align="center"
        prop="experience"
        label="带宠经历"
        width="400"
      />
      <el-table-column
        align="center"
        prop="zhuyi"
        label="注意事项"
        width="400"
      />
      <el-table-column align="center" fixed="right" min-width="200">
        <template #header>
          <div class="header">
            <el-input
              v-model="search"
              size="small"
              placeholder="输入姓名搜索"
            />
            <el-button
              style="margin-left: 10px"
              type="primary"
              size="small"
              @click="addisshow = true"
            >
              添加
            </el-button>
          </div>
        </template>
        <template #default="scope">
          <el-button
            link
            type="primary"
            size="small"
            @click="handleClick(scope.row.people_id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <el-dialog v-model="dialogVisible" title="删除提示" width="500">
    <span>请确认是否删除该人员?</span>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="deletePeople"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
  <el-drawer @close="canceladd" v-model="addisshow" direction="rtl">
    <template #header>
      <h4>添加服务人员</h4>
    </template>
    <template #default>
      <div>
        <el-form :model="formData" ref="FromRef" label-width="auto">
          <el-form-item label="姓名">
            <el-input
              type="text"
              v-model="formData.name"
              placeholder="请输入姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="formData.gender">
              <el-radio value="0" size="large">女</el-radio>
              <el-radio value="1" size="large">男</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否服务1">
            <el-radio-group v-model="formData.is_service1">
              <el-radio value="1" size="large">是</el-radio>
              <el-radio value="0" size="large">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否服务2">
            <el-radio-group v-model="formData.is_service2">
              <el-radio value="1" size="large">是</el-radio>
              <el-radio value="0" size="large">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="是否服务3">
            <el-radio-group v-model="formData.is_service3">
              <el-radio value="1" size="large">是</el-radio>
              <el-radio value="0" size="large">否</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="服务区域">
            <el-radio-group v-model="formData.address">
              <el-radio value="南宁" size="large">南宁</el-radio>
              <el-radio value="深圳" size="large">深圳</el-radio>
              <el-radio value="广州" size="large">广州</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              type="password"
              v-model="formData.password"
              show-password
              placeholder="请输入密码"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="canceladd">取消</el-button>
        <el-button type="primary" @click="add">添加</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, reactive, onBeforeMount, computed } from "vue";
import { getallpeople,addpeople,deletepeople } from "@/service/api/people";
import { ElMessage } from "element-plus";
let tableData = ref([]);
let formData = reactive({
  name:'',
  gender:'1',
  is_service1:'0',
  is_service2:'0',
  is_service3:'0',
  address:'南宁',
  password:''
});
let addisshow = ref(false);
let deleteid = ref("");
let search = ref("");
let dialogVisible = ref(false);
onBeforeMount(() => {
  getpeoples()
});

const filterTableData = computed(() =>
  tableData.value.filter(
    (data) =>
      !search.value ||
      data.name.toLowerCase().includes(search.value.toLowerCase())
  )
);

function getpeoples(){
  getallpeople().then((res) => {
    tableData.value = res.data.data;
  });
}
function add() {
  addpeople({
    name:formData.name,
    gender:formData.gender,
    is_service1:formData.is_service1,
    is_service2:formData.is_service2,
    is_service3:formData.is_service3,
    address:formData.address,
    people_password:formData.password
  }).then((res) => {
    if(res.data.code===200){
      ElMessage({
    message: "添加成功",
    type: "success",
  });
  getpeoples()
    }
  });
}
function canceladd() {
  addisshow.value = false;
}
function deletePeople() {
  deletepeople({people_id:deleteid.value}).then(res=>{
    if(res.data.code===200){
      ElMessage({
    message: "删除成功",
    type: "success",
  });
  dialogVisible.value = false;
  getpeoples()
    }
  })
}
function handleClick(e) {
  deleteid.value = e;
  dialogVisible.value = true;
}
</script>


<style lang="scss" scoped>
.header {
  display: flex;
}
</style>
